package com.liuyucheng.idea_springboot.controller;

import com.liuyucheng.idea_springboot.bean.Result;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @GetMapping("/info")
    public Result<?> info(HttpServletRequest request){
        System.out.println("进入info方法 = " + request);
        Result<?> result = new Result<>();

//            roles: ['admin'],
//    introduction: 'I am a super administrator',
//    avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
//    name: 'Super Admin'
        Map<String,Object> map = new HashMap<>();
        map.put("roles",new String[]{"admin"});
        map.put("introduction","我是超级管理员,上帝模式");
        map.put("avatar","https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fup.enterdesk.com%2Fedpic%2F90%2Fe1%2F8b%2F90e18b391e39310280cedb9dbd87c275.jpg&refer=http%3A%2F%2Fup.enterdesk.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1651330827&t=f3a1b928a5c3113ff64c4d736cdfe683");
        map.put("name","超级管理员");

        return Result.data(map);
    }

    /**
     * 登录
     */
//    @PostMapping(value = "/login")
//    @ResponseBody
//    public Result<?> login(
//            @RequestBody User user,
//            HttpServletRequest request,
//            HttpServletResponse response) {
//        System.out.println("username = " + user.getUsername());
//        System.out.println("password = " + user.getPassword());
//
//        return Result.ok("登录成功");


        // 从SecurityUtils里边创建一个 subject
//        Subject subject = SecurityUtils.getSubject();
//        // 在认证提交前准备 token（令牌）
//        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
//        // 执行认证登陆
//        try {
//            subject.login(token);
//        } catch (UnknownAccountException uae) {
//            return "未知账户";
//        } catch (IncorrectCredentialsException ice) {
//            return "密码不正确";
//        } catch (LockedAccountException lae) {
//            return "账户已锁定";
//        } catch (ExcessiveAttemptsException eae) {
//            return "用户名或密码错误次数过多";
//        } catch (AuthenticationException ae) {
//            return "用户名或密码不正确！";
//        }
//        if (subject.isAuthenticated()) {
//            //将用户信息保存到session中
//            request.getSession().setAttribute("username",username);
//            return "登录成功";
//        } else {
//            token.clear();
//            return "登录失败";
//        }
//    }

    /**
     * 登出  这个方法没用到,用的是shiro默认的logout
     * @param session
     * @param model
     * @return
     */
//    @GetMapping("/logout")
//    public ModelAndView logout(HttpSession session, Model model) {
//        Subject subject = SecurityUtils.getSubject();
//        subject.logout();
//
//        ModelAndView mav = new ModelAndView();
//        mav.setViewName("/login.html");
//        return mav;
//    }

}
